package t999

import (
	"fmt"
	"sync"
)

const (
	_xMax = 8
	_yMax = 8
)

type Point struct {
	x, y int
}

// . = 46    p = 112    R = 82    B = 66
func numRookCaptures(board [][]byte) int {

	var wg sync.WaitGroup

	var Rp Point                //车坐标
	Bp := make(map[string]bool) //大象坐标
	Pp := make(map[string]bool) //卒坐标

	sum := 0

	for x, row := range board {
		for y, el := range row {
			//fmt.Printf(" (%d,%d)%d ", x, y, el)
			switch el {
			case 66:
				Bp[fmt.Sprintf("%d,%d", x, y)] = true
			case 112:
				Pp[fmt.Sprintf("%d,%d", x, y)] = true
			case 82:
				Rp.x = x
				Rp.y = y
			}
		}
	}

	//分别遍历Rp的x,y   1,3  3,3  2,2  2,4
	wg.Add(4)

	{
		go func() {
			for x := Rp.x; x >= 0; x-- {

				tmp := fmt.Sprintf("%d,%d", x, Rp.y)
				if _, ok := Bp[tmp]; ok {
					break
				}
				if _, ok := Pp[tmp]; ok {
					sum++
					break
				}
			}
			wg.Done()
		}()

		go func() {
			for y := Rp.y; y >= 0; y-- {

				tmp := fmt.Sprintf("%d,%d", Rp.x, y)
				if _, ok := Bp[tmp]; ok {
					break
				}
				if _, ok := Pp[tmp]; ok {
					sum++
					break
				}
			}
			wg.Done()
		}()

		go func() {
			for x := Rp.x + 1; x < _xMax; x++ {

				tmp := fmt.Sprintf("%d,%d", x, Rp.y)
				if _, ok := Bp[tmp]; ok {
					break
				}
				if _, ok := Pp[tmp]; ok {
					sum++
					break
				}
			}
			wg.Done()
		}()

		go func() {
			for y := Rp.y + 1; y < _yMax; y++ {

				tmp := fmt.Sprintf("%d,%d", Rp.x, y)
				if _, ok := Bp[tmp]; ok {
					break
				}
				if _, ok := Pp[tmp]; ok {
					sum++
					break
				}
			}
			wg.Done()
		}()
	}

	wg.Wait()

	return sum
}
